/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import Controller.MyThreadClass;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.PropertyConfigurator;
import util.ClientInfoManager;

/**
 *
 * @author
 * Sabbir Ahmed
 */
public class MyServletContextListener implements ServletContextListener {

    private MyThreadClass myThread = null;
    public static ClientInfoManager clientInfoMg;
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        String path = sce.getServletContext().getRealPath("/WEB-INF/log4j.properties");
        PropertyConfigurator.configure(path);
        Logger.getLogger(Server.class.getName()).log(Level.INFO, null, "Server Started");
         if ((myThread == null) || (!myThread.isAlive())) {
            try {
                myThread = new MyThreadClass();
                myThread.start();
                clientInfoMg = new ClientInfoManager();
            } catch (Exception ex) {
                Logger.getLogger(MyServletContextListener.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        try {
            myThread.doShutdown();
            myThread.interrupt();
            Thread.sleep(1000);
        } catch (Exception ex) {
            Logger.getLogger(MyServletContextListener.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}